// pages/report/report.js
import {
  request2
} from "../../utils/request2";


Page({

  /**
   * 页面的初始数据
   */
  data: {
    information: [],
    modalHidden: true,
    devices: [],
    deviceIndex: '#',
    deviceName: '',
    breakType: '',
    mobilePhone: '',
    imgIndex: '',
    srcI: '',
    baseSrc: '',
    whichClick: 0,
    code: '',
    // ifFormFull: false,
    title: '',
    description: '',
    initiator: '',
    deviceCode: '',
    type: '',
    position: '',
    showDeviceInfo: false,
    startPic1: "main/uploadImgsLogo.jpg",
    startPic2: "main/uploadImgsLogo.jpg",
    startPic3: "main/uploadImgsLogo.jpg",
    filesDirectory: 'https://centos.dusin.top:8443/WorkListServer/servlet/web/util/ShowPicture?file=',
  },

  // 图片加载,上传方法
  uploadImgs(e) {
    let index = e.currentTarget.id
    wx.chooseMedia({
      count: 1,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 15,
      sizeType: true,
      camera: 'back',

      success: res => {
        console.log(res)
        this.setData({
          srcI: res.tempFiles[0].tempFilePath,
          imgIndex: index
        })
        switch (this.data.imgIndex) {
          case '1':
            // 上传方法 ，这个name属性是最重要的属性，他对应着后端传参数的值,也就是 MultipartFile file 的file
            // var time = timeUtil.formatTime(new Date());
            let that = this;
            wx.uploadFile({
              filePath: this.data.srcI,
              name: 'img',
              url: 'https://centos.dusin.top:8443/worklist2/upload/uploadimg',
              success(res) {
                console.log(res);
                let resMes = JSON.parse(res.data);
                // const surl = getApp().globalData.serverUrl + "servlet/web/util/ShowPicture?file=main/" + resMes.data;
                const surl = "main/" + resMes.data;
                that.setData({
                  startPic1: surl
                });
              }
            })
            break;
          case '2':
            let _that = this;
            wx.uploadFile({
              filePath: this.data.srcI,
              name: 'img',
              url: 'https://centos.dusin.top:8443/worklist2/upload/uploadimg',
              success(res) {
                console.log(res);
                let resMes = JSON.parse(res.data);
                const surl = "main/" + resMes.data;
                _that.setData({
                  startPic2: surl
                });
              }
            })
            break;
          case '3':
            let __that = this;
            wx.uploadFile({
              filePath: this.data.srcI,
              name: 'img',
              url: 'https://centos.dusin.top:8443/worklist2/upload/uploadimg',
              success(res) {
                console.log(res);
                let resMes = JSON.parse(res.data);
                const surl = "main/" + resMes.data;
                __that.setData({
                  startPic3: surl
                });
              }
            })
            break;
          default:
            console.log("default of switch");
        }
      },
      fail() {
        console.log("图片上传取消")
      }
    })
  },
  // 图片预览方法
  previewImg(e){
    let index = e.currentTarget.id
    console.log(index)
    let filesDirectory = this.data.filesDirectory 
    let startPic1 = this.data.startPic1 
    let startPic2 = this.data.startPic2
    let startPic3 = this.data.startPic3 
    this.setData({
      imgIndex: index
    })
    switch (this.data.imgIndex) {
      case '1':
        if(startPic1 != 'main/uploadImgsLogo.jpg'){
          wx.previewImage({
            urls: [filesDirectory+startPic1], 
          })
        }
        break;
      case '2':
        if(startPic2 != 'main/uploadImgsLogo.jpg'){
          wx.previewImage({
            urls: [filesDirectory+startPic2], 
          })
        }
        break;
      case '3':
        if(startPic3 != 'main/uploadImgsLogo.jpg'){
          wx.previewImage({
            urls: [filesDirectory+startPic3], 
          })
        }
        break;
      default:
        console.log("out of switch !")
        break;
    }

  },
  // 图片重新上传
  reUploadImg(e){
    let index = e.currentTarget.id
    console.log(index)
    this.setData({
      imgIndex: index
    })
    switch (this.data.imgIndex) {
      case '1':
        this.setData({
          startPic1: 'main/uploadImgsLogo.jpg'
        })
        break;
      case '2':
        this.setData({
          startPic2: 'main/uploadImgsLogo.jpg'
        })
        break;
      case '3':
        this.setData({
          startPic3: 'main/uploadImgsLogo.jpg'
        })
        break;
      default:
        break;
    }

  },


  // 设备名称选择器
  bindPickerChange: function (e) {
    console.log(e)
    // console.log(this.data.devices)
    var deviceIndex = e.detail.value 
    this.setData({ 
      position: this.data.devices[deviceIndex].position,
      deviceIndex: e.detail.value,
      deviceCode: this.data.devices[deviceIndex].code,
      showDeviceInfo: true,
      deviceName: this.data.devices[deviceIndex].name
    })
    // console.log(this.data.devices[deviceIndex])
  },
  // 查看设备信息 
  showDeviceInfo(e){
    // console.log(e)
    this.setData({
      whichClick: e.currentTarget.dataset.type
    })
    wx.navigateTo({
      url: '../../pages/reviewReport/reviewReport?whichClick='+
      this.data.whichClick+
      '&deviceCode='+this.data.deviceCode+
      '&code='+this.data.code,

    })
  },

  //表单提交
  formSubmit(e) {
    console.log(e);

    this.setData({
      information: e.detail.value,
      modalHidden: false,
      // task表单数据
      title: e.detail.value.title,
      description: e.detail.value.description,
      initiator: e.detail.value.initiator,
    });
    // 表单完整校验
    if(!this.data.deviceName == '' && !this.data.title == ''){
      // e.detail.target.dataset.type == 1 为保存按钮触发事件
      if (e.detail.target.dataset.type == 1) {
        this.setData({
          whichClick: 1,
        });
      // e.detail.target.dataset.type == 2 为提交按钮触发事件
      } else {
        this.setData({
          whichClick: 2,
        });
      }
    }else{
      wx.showToast({
        title: '必填项未填写',
        icon: 'error',
      })
    }
  },

  //模态框取消
  modalCancel() {
    if (this.data.whichClick == 1) {
      wx.showToast({
        title: '取消保存',
        icon: 'none'
      })
    } else {
      wx.showToast({
        title: '取消提交',
        icon: 'none'
      })
    }
    this.setData({
      modalHidden: true,
    })
  },

  //模态框确定
  modalConfirm: function (e) {
    if (this.data.whichClick == 1) {
      console.log(this.data.whichClick)
      request2({
          url: "/servlet/task/save",
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            initiator: this.data.initiator,
            type: this.data.type,
            title: this.data.title,
            description: this.data.description,
            startPic1: this.data.startPic1,
            startPic2: this.data.startPic2,
            startPic3: this.data.startPic3,
            deviceCode: this.data.deviceCode,
            position: this.data.position
          }
        })
        .then(result => {
          console.log(result);
          this.setData({
          })
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 1500
          })
        })
        .catch(err => { })
    } else if(this.data.whichClick == 2){
      console.log(this.data.whichClick)
      request2({
          url: "/servlet/task/report",
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            initiator: this.data.initiator,
            type: this.data.type,
            title: this.data.title,
            description: this.data.description,
            startPic1: this.data.startPic1,
            startPic2: this.data.startPic2,
            startPic3: this.data.startPic3,
            deviceCode: this.data.deviceCode,
            position: this.data.position
          }
        })
        .then(result => {
          console.log(result);
          this.setData({
          })
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 1500
          })
        })
        .catch(err => {
        })
    }
    this.setData({
      modalHidden: true
    })
    wx.navigateBack({
      url: '/pages/main/main',
    })
  },


  onLoad: function (options) {

    console.log(options)
    const persInfo = wx.getStorageSync('persInfo')
    var typeCode = options.typeCode;
    var code = options.code;
    this.setData({
      task: {
        typeCode: typeCode,
      },
      ['task.initiator']: persInfo.name,
      ['task.mobilePhone']: persInfo.mobilePhone,
      type: typeCode,
      code: code,
    });
    // console.log(this.data.task.initiator) 
    var that = this; //this在函数内部指向变化,所以需要将全局this指向保存到that中
    request2({
        url: '/servlet/device/listbytype',
        data: {
          typeCode: typeCode,
        }
      })
      .then(res => {
        console.log(res)
        that.setData({
          devices: res.data.rows,
        })
        // console.log(this.data.devices)
      }) 
      .catch(err => {

      })
    // console.log(options.typeCode)
    switch (options.typeCode) {
      case '1001':
        this.setData({
          breakType: "电梯故障",
        })
        break;
      case '1002':
        this.setData({
          breakType: "消防故障",
        })
        break;
      case '1003':
        this.setData({
          breakType: "暖通故障",
        })
        break;
      default:
        this.setData({
          breakType: "未添加的故障"
        })
    }
    
  }
})